package com.david.bluetooth.ble.musicctr

import android.content.Context
import android.content.Intent
import android.media.AudioManager
import androidx.core.content.ContextCompat.getSystemService

const val ACTION_TYPE_MUSIC: Byte = 0
const val ACTION_TYPE_VIDEO: Byte = 1
const val ACTION_TYPE_SYSTEM: Byte = 2 //系统指令，音量增大减小，屏幕亮度变大变小


const val MUSIC_ACTION_PLAY: Byte = 16
const val MUSIC_ACTION_PAUSE: Byte = 9
const val MUSIC_ACTION_PREVIOUS: Byte = 11
const val MUSIC_ACTION_NEXT: Byte = 12

const val SYSTEM_ACTION_VOLUME_DOWN: Byte = 0
const val SYSTEM_ACTION_VOLUME_UP: Byte = 1
const val SYSTEM_ACTION_VOLUME_SET: Byte = 2
fun parseResult(context: Context, actionInfo: ByteArray): Boolean {
    val actionType: Byte = actionInfo[0]
    val action: Byte = actionInfo[1]
    var sum:Byte = 0
    for (i in 0 until (actionInfo.size - 1)) {
        sum = (sum + actionInfo[i]).toByte()
    }

    if (sum != actionInfo[actionInfo.size - 1]) {//判断最后校验和
        return false
    }


    when (actionType) {
        ACTION_TYPE_MUSIC -> {
            //when(action) {
            //    MUSIC_ACTION_PLAY ->{
            //
            //    }
            //    MUSIC_ACTION_PLAYPAUSE -> {
            val intent = Intent(Intent.ACTION_VIEW)
            val packageName = "com.david.musicapp" //另一个app的包名
            val className = "com.david.musicapp.PlayService" //另一个app要启动的组件的全路径名
            intent.setClassName(packageName, className)
            intent.putExtra("SONG_ACTION_TYPE_KEY", action.toInt())
            //                    activity.startService(intent);//或者bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 都能启动

            context.startForegroundService(intent)
            //}
            //}

        }

        ACTION_TYPE_SYSTEM -> {
            if (action == SYSTEM_ACTION_VOLUME_DOWN) {
                val audioManager = getSystemService(context, AudioManager::class.java) as AudioManager?
                val currentVolume = audioManager!!.getStreamVolume(AudioManager.STREAM_MUSIC) // 这里的STREAM_MUSIC可以替换为其他音频流类型，比如 STREAM_RING

                if (currentVolume >= 2) {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume - 2, 0); // 这里的STREAM_MUSIC可以替换为其他音频流类型，desiredVolume是你期望设置的音量大小
                }

            } else if (action == SYSTEM_ACTION_VOLUME_UP) {
                val audioManager = getSystemService(context, AudioManager::class.java) as AudioManager?
                val currentVolume = audioManager!!.getStreamVolume(AudioManager.STREAM_MUSIC) // 这里的STREAM_MUSIC可以替换为其他音频流类型，比如 STREAM_RING

                val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)

                if (currentVolume < maxVolume - 2)
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume + 2, 0); // 这里的STREAM_MUSIC可以替换为其他音频流类型，desiredVolume是你期望设置的音量大小
            } else if (action == SYSTEM_ACTION_VOLUME_SET) {
                val audioManager = getSystemService(context, AudioManager::class.java) as AudioManager?
                val currentVolume = audioManager!!.getStreamVolume(AudioManager.STREAM_MUSIC) // 这里的STREAM_MUSIC可以替换为其他音频流类型，比如 STREAM_RING

                val maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)

                val actionValue = if (actionInfo[2] < 0) 0 else if (actionInfo[2] > maxVolume) maxVolume else actionInfo[2]


                if (currentVolume < maxVolume - 2)
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, actionValue as Int, 0); // 这里的STREAM_MUSIC可以替换为其他音频流类型，desiredVolume是你期望设置的音量大小
            }
        }
    }
    return false
}